﻿using JNPF.Basics.Logic.PlatForm.CommonFields;
using JNPF.Basics.Models.PlatForm.Dtos.CommonFields;
using JNPF.Basics.Models.PlatForm.Entities;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 常用字段
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.08.31
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class CommonFieldsController : BaseApiController
    {
        private CommonFieldsBll commonFieldsBll = new CommonFieldsBll();

        #region Get
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(KeywordInput input)
        {
            var data = await commonFieldsBll.GetList();
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(m => m.FieldName.Contains(input.keyword) || m.Field.Contains(input.keyword));
            }
            var output = data.MapToList<CommonFieldsListOutput>();
            return Success(new { list = output });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">请求参数</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await commonFieldsBll.GetInfo(id);
            var output = data.MapTo<CommonFieldsInfoOutput>();
            return Success(output);
        }
        #endregion

        #region POST
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] CommonFieldsCrInput input)
        {
            var entity = input.MapTo<ComFieldsEntity>();
            var isOk = await commonFieldsBll.Create(entity);
            return isOk > 0 ? Success("新增成功") : Error("新增失败");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] CommonFieldsUpInput input)
        {
            var entity = input.MapTo<ComFieldsEntity>();
            var isOk = await commonFieldsBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">请求参数</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await commonFieldsBll.GetInfo(id);
            if (entity != null)
            {
                var isOk = await commonFieldsBll.Delete(entity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            else
            {
                return Error("删除失败");
            }
        }
        #endregion
    }
}
